package sabokan;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import sabokan.game.Trace;

/**
 * Container class that renders the GamePanel
 * @author anaka
 */
public class SabokanFrame extends javax.swing.JFrame {

    /** 
     * Creates new form SokobanFrame
     */
    public SabokanFrame() {
        initComponents();
        this.mainScrollPane.addKeyListener(gamePanel.getGameKeylistener());
        this.mainScrollPane.setFocusable(true);
        this.mainScrollPane.requestFocusInWindow();
        this.gamePanel.addPropertyChangeListener("minimumSize", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                resizeToFitLevel((Dimension) evt.getNewValue());
            }
        });
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                resizeToFitLevel(gamePanel.getMinimumSize());
            }
        });
    }

    /**
     * Resizes all components to the new Dimension
     * @param dim 
     */
    private void resizeToFitLevel(Dimension dim) {
        Dimension newDimension = new Dimension(Math.min(dim.width + 50, Toolkit.getDefaultToolkit().getScreenSize().width), Math.min(dim.height + 50, Toolkit.getDefaultToolkit().getScreenSize().height));
        mainScrollPane.setMinimumSize(newDimension);
        pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainScrollPane = new javax.swing.JScrollPane();
        gamePanel = new sabokan.game.GamePanel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        loadLevelMenuItem = new javax.swing.JMenuItem();
        levelEditorMenuItem = new javax.swing.JMenuItem();
        resetGameMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("sabokanX");

        javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
        gamePanel.setLayout(gamePanelLayout);
        gamePanelLayout.setHorizontalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 772, Short.MAX_VALUE)
        );
        gamePanelLayout.setVerticalGroup(
            gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 671, Short.MAX_VALUE)
        );

        mainScrollPane.setViewportView(gamePanel);

        fileMenu.setText("File");

        loadLevelMenuItem.setText("Load Level");
        loadLevelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadLevelMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(loadLevelMenuItem);

        levelEditorMenuItem.setText("Level Editor");
        levelEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                levelEditorMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(levelEditorMenuItem);

        resetGameMenuItem.setText("Reset Game");
        resetGameMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetGameMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(resetGameMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText("Help");
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 761, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * LoadLevel Action
     * @param evt 
     */
private void loadLevelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadLevelMenuItemActionPerformed
    JFileChooser chooser = new JFileChooser();
    if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
        gamePanel.loadLevel(chooser.getSelectedFile());
    }
}//GEN-LAST:event_loadLevelMenuItemActionPerformed
    /**
     * LevelEditor Action
     * @param evt 
     */
private void levelEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_levelEditorMenuItemActionPerformed
    JDialog levelEditor = new LevelEditor(this);
    levelEditor.setLocationRelativeTo(this);
    levelEditor.setVisible(true);
}//GEN-LAST:event_levelEditorMenuItemActionPerformed
    /**
     * ResetGame Action
     * @param evt 
     */
private void resetGameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetGameMenuItemActionPerformed
    gamePanel.reset();
}//GEN-LAST:event_resetGameMenuItemActionPerformed
    /**
     * Exit Action
     * @param evt 
     */
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
    dispose();
}//GEN-LAST:event_exitMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        Trace.info("Found installed L&F: " + info.getName());
                        if ("Nimbus".equals(info.getName())) {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    Trace.error(e);
                }

                try {
                    new SabokanFrame().setVisible(true);
                } catch (Exception e) {
                    Trace.error(e);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private sabokan.game.GamePanel gamePanel;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem levelEditorMenuItem;
    private javax.swing.JMenuItem loadLevelMenuItem;
    private javax.swing.JScrollPane mainScrollPane;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem resetGameMenuItem;
    // End of variables declaration//GEN-END:variables
}
